<?php
require_once 'BaseController.php';

/**
 * 【PC版】家賃相場(検索結果) コントローラ
 */
class Renew_YachinsobasearchResultController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $cat = $params['Category'];
        if (empty($cat)) $cat = 'index';

        $this->_data['yachinsobatype'] = $params['YachinSobaType'];

        $tatemonotype = substr($params['YachinSobaType'], 0, 3);
        $this->_data['tatemonotype'] = $tatemonotype == '101' ? "賃貸マンション" : ($tatemonotype == '102' ? "賃貸アパート" : "戸建て・テラスハウス");
        $this->_data['tatemonotypecode'] = $tatemonotype == '101' ? "2" : ($tatemonotype == '102' ? "1" : "3");

        $this->_data['madori'] = substr($params['YachinSobaType'], 3, 3);
        $this->_data['area'] = $params['Area'];

        $prefCd = $this->getPrefCode($params['Area']);
        $this->_data['prefnamejp'] = $this->getPrefNameJP($prefCd);

        $params['PrefCd'] = $prefCd;
        $params['type'] = $tatemonotype;
        $params['madori'] = $this->_data['madori'];

        if (empty($params['RailCd']))
        {
            $this->makeTemplateParams1($params);
            $this->_data['resultType'] = 'area';
        }
        else
        {
            $this->makeTemplateParams2($params);
            $this->_data['resultType'] = 'line';
        }

        $this->displayBody2($cat);
    }

    private function makeTemplateParams1($params)
    {
        $model = new Renew_Model_YachinSobaSearch();

        $now = $model->getYachinsoubaDate(1);
        $lastMonth = $model->getYachinsoubaDate(2);

        $lastMonthData = null;

        $nowData = $model->getYachinsoubaCityData($params, 1, $now);

        if (!($nowData == null || $lastMonth == $now))
        {
            $lastMonthData = $model->getYachinsoubaCityData($params, 2, $lastMonth);
        }
        // 表示用データに変換
        $model->convertYachinsouba($nowData, $lastMonthData, $params, $this->_data);
    }

    private function makeTemplateParams2($params)
    {
        $ensenmodel = new Search_Model_Selensen();

        $this->_data['Mapping_ensen'] = $ensenmodel->getRailNameByRailCd($params);

        $model = new Renew_Model_YachinSobaSearch();

        $now = $model->getYachinsoubaDate(1);
        $lastMonth = $model->getYachinsoubaDate(2);

        $lastMonthData = null;

        $nowData = $model->getYachinsoubaEnsenData($params, 1, $now);

        if (!($nowData == null || $lastMonth == $now))
        {
            $lastMonthData = $model->getYachinsoubaEnsenData($params, 2, $lastMonth);
        }
        // 表示用データに変換
        $model->convertYachinsouba($nowData, $lastMonthData, $params, $this->_data);
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        $prefnamejp = $this->_data['prefnamejp'];
        $tatemonoName = $this->_data['tatemonotype'];

        $madoriCode = $this->_data['madori'];
        $madoriName = '1人暮らし';
        if ($madoriCode == '201')
        {
            $madoriName = '1人暮らし';
        }
        else if ($madoriCode == '202')
        {
            $madoriName = '2人暮らし';
        }
        else if ($madoriCode == '203')
        {
            $madoriName = 'ファミリー';
        }

        if ($this->_data['resultType'] == 'line')
        {
            $ensen = $this->_data['Mapping_ensen'][0]['M03_ENSEN_NAME'];

            $this->_meta_title = $prefnamejp . $ensen. $tatemonoName . 'の家賃相場｜賃貸マンション・アパートの賃貸情報 【アパマンショップ】';
            $this->_meta_description = $ensen . 'で' . $tatemonoName .  'の家賃相場から' . $madoriName . 'のお部屋を探すなら、不動産情報が満載のアパマンショップネットワーク。賃貸マンション・アパートなど、';
        }
        else
        {
            $this->_meta_title = $prefnamejp . $tatemonoName . 'の家賃相場｜賃貸マンション・アパートの賃貸情報 【アパマンショップ】';
            $this->_meta_description = $prefnamejp . 'で' . $tatemonoName .  'の家賃相場から' . $madoriName . 'のお部屋を探すなら、不動産情報が満載のアパマンショップネットワーク。賃貸マンション・アパートなど、';
        }

        $this->_meta_keywords = '家賃相場,';
    }

}
?>